<?php
include 'header.php';    
//echo "hello world";
$debug=0;
$fad=$_POST['fad'];
$lad=$_POST['lad'];
$gen=$_POST['gen'];

$label=$_POST['Labels'];
//echo "label is $label<br>";
$radio_sort=$_POST['radio2'];
//echo "radio is $radio_sort";

//echo "gen is $gen";

$arrgen=explode(',', $gen);
$arrfad=explode(',',$fad);
$arrlad=explode(',',$lad);
if($debug){
echo "fad is $fad<br><br>";
echo "lad is $lad<br><br>";
}



if($label =="Labels"){
	for($i=0;$i< sizeof($arrfad);$i++){
		$scatterp[$i]=$arrlad[$i]+($arrfad[$i]-$arrlad[$i])/2;
		//echo "scatterp is $scatterp[$i]<br>";

	}
}

//echo "arrfad size".sizeof($arrfad)."<br>";
//echo "arrlad size".sizeof($arrlad)."<br>";
//echo "scatterp size".sizeof($scatterp)."<br>";

if($radio_sort=="FAD"){
	if($label =="Labels"){
		array_multisort($arrfad, $arrlad, $arrgen,$scatterp);
	}
	else{
		array_multisort($arrfad, $arrlad, $arrgen);
	}
}
if($radio_sort=="LAD"){
	if($label =="Labels"){
		array_multisort($arrlad, $arrfad, $arrgen,$scatterp);
	}
	else{
		array_multisort($arrlad, $arrfad, $arrgen);
	}
}
if($debug){
	for($i=0;$i<sizeof($arrfad);$i++){
		echo "fad is $arrfad[$i] and lad is $arrlad[$i] and gen is $arrgen[$i]<br>";
	}
}

//$File = "../../../admin1/list/graph/rangegraph.php";
$File = "./rangegraph.php";


//$File = "../../../graph/rangegraph.php";
$Handle = fopen($File, 'w');

$Data = "<?php
    include (\"./src/jpgraph.php\");
    include (\"./src/jpgraph_error.php\");
    include (\"./src/jpgraph_scatter.php\");
    
    function _cb_negate(\$aVal) {
		return round(-\$aVal);
	}
    
    \$mygen=\"$gen\";
    \$exploded_mygen=explode(',', \$mygen);
    
    
    
    ";
    fwrite($Handle, $Data);
	if($label =="Labels"){
		$Data="\$scattery=array";
		fwrite($Handle, $Data);
		$scatterbar="($scatterp[0]";
		for($i=1;$i<sizeof($scatterp);$i++){
			$scatterbar=$scatterbar.",".$scatterp[$i];
		}
		$scatterbar=$scatterbar.")";
		$len=sizeof($scatterp);
		$Data="$scatterbar;
		
		for(\$i=0; \$i <$len ; ++\$i ) {
	    \$targ1[\$i] = \"http://localhost\"; 	    
	    \$alts1[\$i] = \"genus=\$exploded_mygen[\$i]\";
	    
		}
		
		\$n = count(\$scattery);
		for(\$i=0; \$i<\$n; ++\$i) {
			\$scattery[\$i] = round(-\$scattery[\$i]);
		}
		
		
		
		";
		fwrite($Handle, $Data);
	}
    

    $Data ="\$datay=array";
    fwrite($Handle, $Data);
    //if($radio_sort == "FAD" || $radio_sort == "LAD"){
		//$barval="($arrfad[1],$arrlad[1]";
		//for($tmp=2; $tmp<sizeof($arrfad); $tmp++){    
		//$barval =$barval.",".$arrfad[$tmp].",".$arrlad[$tmp];
		//}
    //}
    //else{
		$barval="($arrfad[0],$arrlad[0]";
		for($tmp=1; $tmp<sizeof($arrfad); $tmp++){    
		$barval =$barval.",".$arrfad[$tmp].",".$arrlad[$tmp];
		}
    
    
    //}
    $barval=$barval.")";
    if($debug) {echo "barvalue is".$barval;}
    $Data="$barval;


	\$n = count(\$datay);
	for(\$i=0; \$i<\$n; ++\$i) {
		\$datay[\$i] = round(-\$datay[\$i]);
	}

    
    \$graph = new Graph(600,400,\"auto\");    
	\$graph->SetScale(\"textlin\");

	\$graph->img->SetMargin(40,30,20,80);
	\$graph->SetShadow();
	
	\$errplot=new ErrorPlot(\$datay);
	\$errplot->SetColor(\"red\");
	\$errplot->SetWeight(2);
	\$errplot->SetCenter();

	
	\$graph->Add(\$errplot);
	
	";
	fwrite($Handle,$Data);
	if($label=="Labels"){
		$Data="
		\$sp1 = new ScatterPlot(\$scattery,\$datax);
		\$sp1->SetCSIMTargets(\$targ1,\$alts1);

		\$graph->Add(\$sp1);";
		fwrite($Handle,$Data);
	}

	$Data="
	

	\$graph->title->Set(\"Individual Genera\");
	\$graph->xaxis->title->Set(\"Individual Genera\");
	\$graph->yaxis->title->Set(\"Time: M.Y.\");

	\$graph->title->SetFont(FF_FONT1,FS_BOLD);
	\$graph->yaxis->SetLabelFormatCallback(\"_cb_negate\");

	\$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
	\$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
	\$graph->xaxis->Hide();
	//\$graph->xaxis->SetTextLabelInterval(36339);
	\$datax = \$gDateLocale->GetShortMonth();
	\$graph->xaxis->SetTickLabels(\$datax);";
	fwrite($Handle,$Data);
	
	if($label=="Labels"){
	$Data="
	//\$graph->StrokeCSIM('auto');
	//\$graph->Stroke('../../../admin1/list/graph/jpgraph_cache/rangegraph.png');
	\$graph->Stroke('./jpgraph_cache/rangegraph.png');
	//\$graph->Stroke('/var/www/fossilplot/test/graph/jpgraph_cache/rangegraph.png');
	\$imgmap = \$graph->GetHTMLImageMap('scattermap');
	echo \$imgmap;
	//echo \"<img src='http://www.fossilplot.org/test/graph/jpgraph_cache/rangegraph.png' ISMAP USEMAP='#scattermap' border='0'>\";
	//echo \"<img src='http://localhost/test/admin1/list/graph/jpgraph_cache/rangegraph.png' ISMAP USEMAP='#scattermap' border='0'>\";
	echo \"<img src='http://www.fossilplot.org/test/graph/jpgraph_cache/rangegraph.png' ISMAP USEMAP='#scattermap' border='0'>\";

	
	
	?>";
	fwrite($Handle, $Data);
	
	}
	else{
	$Data="
	\$graph->Stroke();
    
    ?>";  
    
    
    fwrite($Handle, $Data);
    }
    
   fclose($Handle);
    //$myFile1 = "../../../graph/jpgraph_cache/rangegraph.png";
    $myFile1 = "./jpgraph_cache/rangegraph.png";
    
  if(file_exists($myFile1)){
	unlink($myFile1);
  }
  $myFile = "./jpgraph_cache/str_range1.png";
    
  if(file_exists($myFile)){
	unlink($myFile);
  }
  
  
  
  //flush();
   //echo "<img src=\"http://www.fossilplot.org/test/graph/rangegraph.php\">";
   //require('../../../admin1/list/graph/rangegraph.php');
   if($label =="Labels"){
	 require('rangegraph.php');
	}
	else{
		//echo "<img src=\"http://localhost/test/admin1/list/graph/rangegraph.php\">";
		echo "<img src=\"http://www.fossilplot.org/test/graph/rangegraph.php\">";
	}
   

  
  include 'footer.php';
  
  

?>